// pages/money/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    txsuc: true,
    txfail: true,
    contact: true,
    money: 0,
    is_subscribe: '', 
    inputValue:'',
    count:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const that = this;
    var obj = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/redpacket/Withdrawal/is_subscribe',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: "POST",
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function(res) {
        console.log(res)
        that.data.is_subscribe = res.data.is_subscribe
      }
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const that = this;
    var obj = {
      unionid: wx.getStorageSync('openid'),
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/redpacket/Theme/cash_out',
      data: {
        unionid: wx.getStorageSync('openid'),
        sign: sign
      },
      method: "POST",
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function(res) {
        console.log(res)
        that.setData({
          money: res.data.content,
          count: res.data.count
        })
      }
    })
  },
  onUnload:function(){
    let pages = getCurrentPages();
    let prevPage = pages[0]
    if (prevPage.route == "pages/tarbar/my/index") {
      prevPage.setData({
        isShowInterstitialAd: 100,
      })
    }
  },
  lookMoney: function() {
    wx.navigateTo({
      url: '/pages/other/moneyDetail/index',
    })
  },
  cashAmount: function(e) {
    const that = this;
    var form_id = e.detail.formId;
    var inputmoney = Number(e.detail.value.money)
    if (typeof inputmoney === 'number' && inputmoney % 1 === 0) {
      if (that.data.money >= inputmoney && inputmoney >= 2 && inputmoney<=10) {
        if (that.data.is_subscribe == 1) {
          var obj = {
            unionid: wx.getStorageSync('openid'),
            amount: inputmoney,
            formid: form_id,
          }
          var sign = app.sort_ASCII(obj)
          wx.request({
            url: app.globalData.new_url + '/redpacket/Withdrawal/index',
            data: {
              unionid: wx.getStorageSync('openid'),
              amount: inputmoney,
              formid: form_id,
              sign: sign
            },
            method: "POST",
            header: {
              'Cache-Control': 'max-age=60,public', //60秒
            },
            success: function(res) {
              console.log(res)
              if (res.data.code ==200){
                that.setData({
                  txsuc:false,
                  inputValue: ''
                })
              }else if(res.data.code==400){
                wx.showModal({
                  title: '提示',
                  content:res.data.msg,
                  showCancel: false,
                })
              }
              that.onShow();
            }
          })
        }else{
          that.setData({
            txfail:false
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '请输入正确金额',
          showCancel: false,
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '请输入正确金额',
        showCancel: false,
      })
    }
  },
  hideHandle: function() {
    const that = this;
    that.setData({
      txsuc: true,
      contact: true,
      txfail: true,
    })
  },
  gzclick: function() {
    const that = this;
    that.setData({
      txfail: true,
      contact: false,
    })
  },

})